const Business = require('../models/business');
const businessClass = require('../models/businessClass');
const listActivities = require('../models/listActivities');
const {
    Op
} = require('sequelize');
// 查询商家列表
exports.findAll = async function(select, businessClass, pagenum = 1, pagesize = -1) {
        const total = await Business.count();
        limit = pagesize == -1 ? total : pagesize;
        const lists = await Business.findAll({
            attributes: [...select],
            where: {
                businessClass: {
                    [Op.like]: businessClass ? businessClass : '%'
                }
            },
            offset: (pagenum - 1) * limit,
            limit: +limit
        })
        return {
            lists,
            total
        }
    }
    // 查询商家信息
exports.findData = async function(id) {
    const result = await Business.findAll({
        where: {
            id
        }
    })
    return result
}

//更新支持信息
exports.upDateSupport = async function(id, obj) {
    const result = await Business.update(obj, {
        where: {
            id
        }
    })
    return result
}

// 增加商家信息
exports.addBusiness = async function(params) {
    let [result] = await Business.findAll({
        where: {
            business: params.business
        }
    })
    let result3;
    if (result == undefined) {
        result3 = await Business.create(params)
    }

    const result2 = await listActivities.findAll({
        where: {
            business: params.business
        }
    })
    let [result4] = await Business.findAll({
        where: {
            business: params.business
        }
    })
    if (result2 != false) return false
    listActivities.create({ business: result4.business, businessId: result4.id });
    return result4
}

<<<<<<< HEAD
//查询商家全部分类
exports.findClass=async function(){
    const result=await businessClass.findAll({
        attributes:['id','classLists']
=======
//查询商家分类
exports.findClass = async function() {
    const result = await businessClass.findAll({
        attributes: ['id', 'classLists']
>>>>>>> 001
    })
    return result
}

//查询活动商家
exports.findActivities = async function(pagenum = 1, pagesize = -1) {
    const total = await listActivities.count();
    limit = pagesize == -1 ? total : pagesize;
    const result = await listActivities.findAll({
        attributes: ['id', 'business', 'businessId'],
        offset: (pagenum - 1) * limit,
        limit: +limit
    });
    return {
        result,
        total
    }
}